package com.servlet.demo; /**
 * @author wzm
 * @create 2023-06-25 11:30
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("这里是登录的逻辑");

        //1.设置请求与响应的编码方式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2.获取页面携带的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("username = " + username);
        System.out.println("password = " + password);

        //3.通过相关的内容去查询 当前用户名和密码是否与数据库一致
         boolean flag = false; //需要通过数据库动态查询


        //4.一致则返回登录成功 否则返回登录失败
        if(flag){
            //response.getWriter().write("登录成功！");
            //到首页
            request.getRequestDispatcher("/index.jsp").forward(request,response);

        }else{
            // response.getWriter().write("登录失败！");
            //继续停留登录页 转发到html会出现问题
            request.getRequestDispatcher("/login.html").forward(request,response);
        }


    }
}
